package com.sumer.brain.sys.controller;

import com.google.code.kaptcha.Producer;
import com.sumer.brain.common.model.R;
import com.sumer.brain.sys.model.entity.SysUser;
import com.sumer.brain.sys.model.req.LoginUserInfoReq;
import com.sumer.brain.sys.model.req.WechatLoginReq;
import com.sumer.brain.sys.model.resp.CaptchaResp;
import com.sumer.brain.sys.service.LoginService;
import com.sumer.brain.sys.service.SysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.UUID;

@RequiredArgsConstructor
@RestController
@Tag(name = "登录")
public class LoginController {

    private final LoginService loginService;

    private final Producer kaptchaProducer;

    private final SysUserService sysUserService;

    @Operation(summary = "用户登录", description = "用户登录", tags = "登录")
    @PostMapping("/login")
    public R<String> login(@RequestBody LoginUserInfoReq user) {
        return R.ok(loginService.login(user));
    }
    
    @Operation(summary = "微信登录", description = "微信登录", tags = "登录")
    @PostMapping("/wechat/login")
    public R<String> wechatLogin(@RequestBody WechatLoginReq wechatLoginReq) {
        return R.ok(loginService.wechatLogin(wechatLoginReq));
    }

    @Operation(summary = "退出登录", description = "退出登录", tags = "登录")
    @GetMapping("/logout")
    public R<String> logout() {
        // 暂未实现，退出功能交给前端
        return R.ok();
    }

    @Operation(summary = "注册用户", description = "新增用户", tags = "新增")
    @PostMapping("/register")
    public R<Boolean> register(@RequestBody @Validated SysUser sysUser) {
        return R.ok(sysUserService.add(sysUser));
    }

    @Operation(summary = "验证码", description = "验证码", tags = "登录")
    @GetMapping("/captcha")
    public R<CaptchaResp> captchaImageProducer(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 设置浏览器不缓存此图片
        response.setHeader("Cache-Control", "no-store, no-cache");
        response.setContentType("image/jpeg");
        String capText = kaptchaProducer.createText();
        // 生成验证码图片
        BufferedImage bi = kaptchaProducer.createImage(capText);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(bi, "png", baos); // 可以改为需要的图片格式，如"jpg"
        byte[] imageBytes = baos.toByteArray();
        UUID uuid = UUID.randomUUID();
        String u = uuid.toString().replace("-", "");
        return R.ok(new CaptchaResp(u, Base64.getEncoder().encodeToString(imageBytes), true));
    }
}
